import { Body, Controller, Get, Post, Res, Response, Query, All } from '@nestjs/common';
import { MediaCheck } from './mediaCheck.entity';
import { MediaCheckService } from './mediaCheck.service';
import { WXAPI } from 'src/common/wx_api';
const crypto = require('crypto')


import {
  apibefore
} from '../../router'
import { TraceRelaTableService } from '../traceRelaTable/traceRelaTable.service';
import { TraceRelaTable } from '../traceRelaTable/traceRelaTable.entity';
import { MultiImgService } from '../multiImg/multiImg.service';
import { MultiImg } from '../multiImg/multiImg.entity';
import { AnswerInfoService } from '../answerInfo/answerInfo.service';
import { AnswerInfo } from '../answerInfo/answerInfo.entity';
import { AnswerService } from '../answer/answer.service';
@Controller(apibefore('/mediaCheck'))
export class MediaCheckController {
  constructor(private readonly service: MediaCheckService,
    private readonly traceRelaTableService: TraceRelaTableService,
    private readonly multiImgService: MultiImgService,
    private readonly answerInfoService: AnswerInfoService,
    private readonly answerService: AnswerService,
    ) {}
  
  @All('/mediaCheckAsync')
  async mediaCheckAsync(@Body() data, @Query() params, @Response() res) {
    console.log(data,'body')
    console.log(params,'query')
    let signature = params.signature,
      timestamp = params.timestamp,
      nonce = params.nonce,
      echostr = params.echostr;
    const wxapi = new WXAPI()
    await wxapi.getAccessToken()
    let a = crypto.createHash('sha1').update([wxapi.token, timestamp, nonce].sort().join('')).digest('hex'); // 这里的pushToken就是在上面的那里配置的Token
    if(a == signature){
      if(data.trace_id){
        //添加一条腾讯验证信息
        const addData = new MediaCheck()
        addData.appid = data.appid
        addData.to_user_name = data.ToUserName
        addData.from_user_name = data.FromUserName
        addData.create_time = data.CreateTime
        addData.msgtype = data.MsgType
        addData.event = data.Event
        addData.trace_id = data.trace_id
        addData.version = data.version
        addData.suggest = data.result.suggest
        addData.label = data.result.label
        const detail = data.detail[0] || {}
        addData.strategy = detail.strategy
        addData.prob = detail.prob
        await this.service.create(addData)
        //查询验证信息关联的是视频、多图、答案图片
        let findTraceInfo:TraceRelaTable = await this.traceRelaTableService.findOne_By_Trace_Id(data.trace_id)
        switch(findTraceInfo.table){
          case 'video'://作品
            //驳回
            if(addData.suggest === 'risky' || addData.suggest === 'review'){
              await this.service.updateVideoCheckText_By_Trace_id(addData.trace_id,0)
            }
            break;
          case 'multi_img'://多图
            if(addData.suggest === 'risky' || addData.suggest === 'review'){
              let findVideoId:MultiImg = await this.multiImgService.findVideoId_By_Trace_Id(data.trace_id)
              if(findVideoId){
                //驳回
                await this.service.updateVideoCheckText_By_id(findVideoId.video_id,0)
              }
            }
            break;
          case 'answer_info'://回答图片
            if(addData.suggest === 'risky' || addData.suggest === 'review'){
              let findVideoId:AnswerInfo = await this.answerInfoService.findOne_By_TraceId(data.trace_id)
              if(findVideoId){
                await this.answerService.check_by_id(findVideoId.answer_id, 0)
              }
            }
            break;
        }
      }
      // 如果验证成功则原封不动的返回
      res.send(echostr);
    }else{
      res.send({
        status: 400,
        data: "check msg error"
      })
    }
  }
}
